【问题标题】:Python Web Scraping With ProblemsPython Web Scraping 有问题
【发布时间】:2020-08-14 12:36:02
【问题描述】:

我正在使用requests-HTML和beautiful来抓取一个网站,下面是代码。奇怪的是,有时我可以在使用 print(soup.get_text()) 时从网上获取文本,而在使用 print(soup) 时,我会得到一些随机代码 - 在所附图像中。

session = HTMLSession()
r = session.get(url)
soup = bs(r.content, "html.parser")
print(soup.get_text())
#print(soup)

The program return this when I tried to look at the soup

【问题讨论】:

    标签: python web screen-scraping


    【解决方案1】:

    我认为该网站受 javascript 保护..试试这个..它可能会有所帮助

    import requests
    from bs4 import BeautifulSoup
    
    r = requests.get(url)
    print(r.text)
    
    #if you want the whole content you can just do slicing stuff on the response stored in r or rather just do it with bs4
    
    soup = BeautifulSoup(r.text, "html.parser")
    print(soup.text)
    

    【讨论】:

    • 感谢您的建议。我确实尝试了请求,r.text 不会返回任何内容,有时会返回状态码 724。
    猜你喜欢
    • 2021-12-25
    • 1970-01-01
    • 2018-04-07
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2012-04-12
    • 2023-02-24
    相关资源
    最近更新 更多