【问题标题】:Need help in Python web scraping在 Python 网页抓取方面需要帮助
【发布时间】:2020-03-23 00:22:50
【问题描述】:

我编写了一个简单的代码来抓取标题、地址、联系人、电话号码和网站链接,但我的程序只是抓取标题,我不知道如何抓取所有其他内容,因为它们没有类和 ID。

这是我的代码:

import requests
from bs4 import BeautifulSoup
import csv

def get_page(url):
    response = requests.get(url)

    if not response.ok:
        print('server responded:', response.status_code)
    else:
        soup = BeautifulSoup(response.text, 'html.parser')
    return soup

def get_detail_data(soup):

    try:
        title = soup.find('a',class_="ListingDetails_Level1_SITELINK",id=False).text
    except:
        title = 'empty'  
    print(title)
    try:
        address = soup.find('div',class_="ListingDetails_Level1_CONTACTINFO",id=False).find_all('span').text
    except:
        address = "address"
    print(address)
    try:
        person_name = soup.find('a',class_="",id=False).find_all('img').text
    except:
        person_name = "empty person"
    print(person_name)
    try:
        phone_no = soup.find('img',class_="",id=False).text
    except:
        phone_no = "empty phone no"
    print(phone_no)
    try:
        website = soup.find('a',class_="",id=False).text
    except:
        website = "empty website"
    print(website)




def main():
    url = "https://secure.kelownachamber.org/Pools-Spas/Rocky%27s-Reel-System-Inc-4751"
    #get_page(url)
    get_detail_data(get_page(url))
if __name__ == '__main__':
    main()

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:

以下代码对我有用(这只是为了向您展示如何从该网站获取数据,所以我保持简单):

import requests
from bs4 import BeautifulSoup
result = requests.get("https://secure.kelownachamber.org/Pools-Spas/Rocky%27s-Reel-System-Inc-4751")
src = result.content
soup = BeautifulSoup(src,'html.parser')
divs  = soup.find_all("div",attrs={"class":"ListingDetails_Level1_HEADERBOXBOX"})
for tag in divs:
  try:
   title = tag.find("a",attrs={"class":"ListingDetails_Level1_SITELINK"}).text
   address = tag.find("span",attrs={"itemprop":"street-address"}).text
   postal = tag.find("span",attrs={"itemprop":"postal-code"}).text
   maincontact = tag.find("span",attrs={"class":"ListingDetails_Level1_MAINCONTACT"}).text
   siteTag = tag.find("span",attrs={"class":"ListingDetails_Level1_VISITSITE"})
   site = siteTag.find("a").attrs['href']
   print(title)
   print(address) 
   print(postal)
   print(maincontact)
   print(site)
  except:
   pass

【讨论】:

  • 谢谢兄弟,能否设置一个循环,以便程序抓取所有列表的数据?请在这里是主页的链接:secure.kelownachamber.org/Automotive-Sales-Service
  • 你好 M.Akram 你好亲爱的 Pulkit - 非常感谢 - 我在 Atom 中的 MX Linux 上运行它,它运行良好 - 它返回以下内容:` Rocky's Reel System Inc. 8 - 730 Stremel Road V1X 5E7 杰夫汉考克 1-800-663-8705 | 1-604-985-0525 rockys.ca [在 4.632 秒内完成]` - 非常感谢这个伟大的线程。我目前正在学习 python,这是一个很好的资源。 - 我很高兴在学习上得到您的支持。
【解决方案2】:

如果您尝试使用 Beautiful Soup 抓取的页面元素没有类或 ID,则可能很难告诉 find() 方法您要查找的内容。

在这种情况下,我更喜欢使用select()select_one(),它们记录在here 中。这些方法允许您传递 CSS 选择器 - 与您用来告诉 Web 浏览器您想要以特定方式设置哪些元素样式的语法完全相同。

您可以参考here 找到可供您使用的选择器。我无法为您提供您的案例所需的确切 CSS 表达式,因为您没有提供您尝试抓取的 HTML 示例,但这应该可以帮助您开始。

例如,如果您尝试抓取的页面如下所示:

<div id="contact">
    <div>
        <a href="ListingDetails_Level1_SITELINK">Some title</a>
    </div>

    <div>
        <p>1, Sesame St., Address...... </p>
    </div>
</div>

然后要获取地址,您可以使用 CSS 选择器,如下所示:

address = soup.select_one("#contact > div:nth-child(2) > p")

上面说地址将通过立即在具有 id 'contact' 的 div 中的第二个 div 中查找,然后立即在其中的段落中查找。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2020-08-29
    • 2017-12-05
    相关资源
    最近更新 更多