簡體   English   中英

如何在抓取 html 頁面時找出正確的 div、class、跨度

[英]How can I find out correct div, class, span when scraping a html page

我是Web刮技術的新手。 在閱讀了諸如此類各種 web 教程后,我嘗試實現Web 抓取。 這些文章是關於亞馬遜 web 抓取和 Netflix web 抓取的文章。 Imdb、爛番茄等還有很多其他教程。 這些教程概述了需要采用哪些屬性,例如class attributes, div tags等。不同的網站有不同的方法來獲取這些標簽。 然而,這些標簽是 web 抓取的基本元素。 當我按照這些教程進行操作時,我可以實現這些代碼,但是當我嘗試解析上述網站以外的其他網站時,我失敗了。 最近,我嘗試了priceline上的代碼塊。 但我只是搞砸了這么多 html 代碼。

我的價格線代碼

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36", "Accept-Encoding":"gzip, deflate", "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "DNT":"1","Connection":"close", "Upgrade-Insecure-Requests":"1"}
url= 'https://www.priceline.com/relax/in/3000005381/from/20210301/to/20210319/rooms/1?vrid=8848a774a531423bde3ed4ff3486f8bb'
r = requests.get(url, headers=headers)#, proxies=proxies)
content = r.content
soup = BeautifulSoup(content)

name=[]
hotel_div = soup.find_all('div', class_='Box-sc-8h3cds-0.Flex-sc-1ydst80-0.iNmVhl')
for container in hotel_div:
   name = d.find('span', attrs={'class':'Box-sc-8h3cds-0 Flex-sc-1ydst80-0 BadgeRow__BadgeContainer-fofgl-0 kmpPcP SummaryHeader__BadgeRowWithMB-m5g1dm-0 dQyPUf SummaryHeader__BadgeRowWithMB-m5g1dm-0 dQyPUf'})
   n = name.find_all('img', alt=True)
   row={}
   if name is not None:
     #print(n[0]['alt'])
     row['Name'] = n[0]['alt']
   else:
      row['Name'] = "unknown-product"
print(name)

它返回一個空數組。

任何人都可以建議任何教程或 web 博客來幫助我識別任何網站的正確 html 標簽嗎?

感謝您的幫助

每個 web 開發人員都會選擇以不同的方式命名他們的類和標簽。

要檢查新站點的結構,您可以右鍵單擊要抓取的內容,然后單擊檢查,然后會出現一個選項卡,您可以在其中找到標簽、class 名稱等

更新)現在它可以工作了:

    import re
    from bs4 import BeautifulSoup as soup
    import requests
    from selenium import webdriver
    url = 'https://www.priceline.com/relax/in/3000005381/from/20210301/to/20210319/rooms/1?vrid=04bab06455d612983ec0c76e621d7c48'
    driver = webdriver.Chrome()
    driver.get(url)
    html = driver.page_source
    soup = soup(html,"lxml")
    container = soup.find('a',{'class':'Link-sc-16qjtx7-0 TitleLink__TitleLinkText-vs18lp-0 jtrNVn'}).text
    print(container)

https://i.stack.imgur.com/IJK0c.png

從<span>沒有 class 的 a 中刮取</span>一個“href”<div></div><div id="text_translate"><p> 我對使用 BeatifulSoup 和 python 進行抓取非常陌生,我在嘗試在跨度內獲取 href 時遇到了一些困難,但它沒有 class .. 以下部分代碼來自 phpbb 論壇,我可以毫無問題地抓取所有 href但由於某種原因,我無法弄清楚如何抓住跨度內的東西..</p><pre> &lt;div class="col-md-48 post-text" data-topic="6693rw38" data-forum="2"&gt; &lt;br&gt; &lt;br&gt; &lt;a href="http://imgshare.net/img-5ba3dt3ad8a24.html" target="_blank" class="postlink" rel="nofollow"&gt;&lt;/a&gt; &lt;br&gt; &lt;br&gt; &lt;a href="http://imgshare.net/img-5baefr1a51a49.html" target="_blank" class="postlink" rel="nofollow"&gt;&lt;/a&gt; &lt;br&gt; &lt;br&gt; &lt;span&gt; &lt;a href="https://k2s.cc/file/5c745ce5g9193/toyota.mp4" target="_blank"&gt;https://k2s.cc/file/5c745ce5g9193/toyota.mp4&lt;/a&gt; &lt;/span&gt; &lt;br&gt; &lt;br&gt; &lt;a href="http://imgshare.net/img-5ba34d1q805b8.html" target="_blank" class="postlink" rel="nofollow"&gt;&lt;/a&gt; &lt;br&gt; &lt;br&gt; &lt;span&gt; &lt;a href="https://k2s.cc/file/b28gr283ef76e/ford.mp4" target="_blank"&gt;https://k2s.cc/file/b28gr283ef76e/ford.mp4&lt;/a&gt; &lt;/span&gt;</pre><p> 這將為我提供 a 標簽內的所有“href”:</p><pre> url ='somephpbbforum.com' page = requests.get(url) soup = BeautifulSoup(page.content, 'lxml') link = soup.find_all('div', class_ = 'col-md-48') for div in link: all_links = [link1['href'] for link1 in div.find_all('a')] print(all_links)</pre><p> 多謝你們!</p></div>

[英]Scraping a 'href' from a <span> with no class inside a<div>

我該如何選擇

[英]How can I select <div data-image='…“”> in python if I want to scraping image out with Python

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 我如何提取沒有 class 的(.find())div(或跨度) 刮除Div類HTML時如何在if循環內嵌套if語句 BeautifulSoup 抓取跨度類 HTML Web div class 中 span 標簽的抓取 從<span>沒有 class 的 a 中刮取</span>一個“href”<div></div><div id="text_translate"><p> 我對使用 BeatifulSoup 和 python 進行抓取非常陌生,我在嘗試在跨度內獲取 href 時遇到了一些困難,但它沒有 class .. 以下部分代碼來自 phpbb 論壇,我可以毫無問題地抓取所有 href但由於某種原因,我無法弄清楚如何抓住跨度內的東西..</p><pre> &lt;div class="col-md-48 post-text" data-topic="6693rw38" data-forum="2"&gt; &lt;br&gt; &lt;br&gt; &lt;a href="http://imgshare.net/img-5ba3dt3ad8a24.html" target="_blank" class="postlink" rel="nofollow"&gt;&lt;/a&gt; &lt;br&gt; &lt;br&gt; &lt;a href="http://imgshare.net/img-5baefr1a51a49.html" target="_blank" class="postlink" rel="nofollow"&gt;&lt;/a&gt; &lt;br&gt; &lt;br&gt; &lt;span&gt; &lt;a href="https://k2s.cc/file/5c745ce5g9193/toyota.mp4" target="_blank"&gt;https://k2s.cc/file/5c745ce5g9193/toyota.mp4&lt;/a&gt; &lt;/span&gt; &lt;br&gt; &lt;br&gt; &lt;a href="http://imgshare.net/img-5ba34d1q805b8.html" target="_blank" class="postlink" rel="nofollow"&gt;&lt;/a&gt; &lt;br&gt; &lt;br&gt; &lt;span&gt; &lt;a href="https://k2s.cc/file/b28gr283ef76e/ford.mp4" target="_blank"&gt;https://k2s.cc/file/b28gr283ef76e/ford.mp4&lt;/a&gt; &lt;/span&gt;</pre><p> 這將為我提供 a 標簽內的所有“href”:</p><pre> url ='somephpbbforum.com' page = requests.get(url) soup = BeautifulSoup(page.content, 'lxml') link = soup.find_all('div', class_ = 'col-md-48') for div in link: all_links = [link1['href'] for link1 in div.find_all('a')] print(all_links)</pre><p> 多謝你們!</p></div> 雖然 web 在 BeautifulSoup 中抓取,但<span>如果特定標簽沒有 class 或 id,我該如何引用它?</span> 我該如何選擇 找不到 div 類的 Web 抓取選擇器 通過關系添加SQLAlchemy模型時,如何找到要使用的正確類? 如何使用正確的路由通過Flask顯示HTML頁面?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM