【问题标题】:why can't I access full html of this page using urllib, beautifulsoup为什么我不能使用 urllib、beautifulsoup 访问此页面的完整 html
【发布时间】:2018-04-16 12:58:37
【问题描述】:

我是网络抓取的新手,出于学习目的,我想在https://retty.me/ 网站中找到所有 href 链接。 但我发现我的代码在那个网站上只能找到一个链接。但是我查看了页面源代码,它有很多没有打印的链接。我还打印只有一个链接包含的整页。 我做错了什么?

请纠正我。

这是我的python代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
data=[]
html = urlopen('https://retty.me')
soup = BeautifulSoup(html,'lxml')
print(soup)
for link in soup.findAll('a', attrs={'href': re.compile("^https://")}):
    data.append(link.attrs['href'])



file=open('scraped_data.txt','w')
for item in data:
    file.write("%s\n"%item)
file.close()
 

【问题讨论】:

    标签: python beautifulsoup urllib


    【解决方案1】:

    如果您输入 html 中显示的消息,您会进入谷歌翻译,它会显示“我们为您的麻烦道歉”。 他们不希望人们抓取他们的网站,因此他们根据用户代理过滤请求。你只需要在请求头中添加一个用户代理,看起来像一个浏览器。

    from urllib.request import urlopen, Request
    from bs4 import BeautifulSoup
    import re
    
    data=[]
    
    url = 'https://retty.me'
    req = Request(
        url, 
        data=None, 
        headers={
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
        }
    )
    html = urlopen(req)
    soup = BeautifulSoup(html,'lxml')
    print(soup)
    for link in soup.findAll('a', attrs={'href': re.compile("^https://")}):
        data.append(link.attrs['href'])
    
    for item in data:
        print(item)
    

    事实上,这个特定站点只需要存在用户代理标头,并且可以接受任何用户代理,即使是空字符串。 Rishav 提到的 requests 库默认提供了一个用户代理,这就是为什么它可以在不添加自定义标头的情况下工作。

    【讨论】:

    • 我还有一个问题。那个 User-Agent 参数是固定的吗?我到处都有几乎相同的参数。谢谢。
    • 你可以使用任何用户代理。通常,发出请求的应用程序会放置一些东西来标识自己,例如“Mozilla ...”或“Chrome ...”或“python-requests ...”。网站可能会根据用户代理字段过滤请求,因此对于抓取,最好让它看起来像抓取工具是一个网络浏览器。例如,您可以在网络选项卡中查看 chrome 使用的用户代理。
    【解决方案2】:

    我不知道为什么网站在使用 urllib 时返回不同的 HTML,但是您可以使用出色的 requests 库,它比 urllib 更容易使用。

    from bs4 import BeautifulSoup
    import re
    import requests
    
    data = []
    html = requests.get('https://retty.me').text
    soup = BeautifulSoup(html, 'lxml')
    for link in soup.findAll('a', attrs={'href': re.compile("^https://")}):
        data.append(link.attrs['href'])
    print(data)
    

    【讨论】:

      【解决方案3】:

      您可以找到请求here 和Beautiful Soup here 的官方文档。

      import requests
      from bs4 import BeautifulSoup
      
      # your Response object called response
      response = requests.get('https://retty.me')
      
      # your html as string
      html = response.text
      
      #verify that you get the correct html code
      print(html)
      
      #make the html, a soup object
      soup = BeautifulSoup(html, 'html.parser')
      
      # initialization of your list
      data = []
      
      # append to your list all the URLs found within a page’s <a> tags
      for link in soup.find_all('a'):
          data.append(link.get('href'))
      
      #print your list items
      print(data)
      

      【讨论】:

        猜你喜欢
        • 2019-04-05
        • 1970-01-01
        • 1970-01-01
        • 2014-10-27
        • 2018-11-24
        • 2019-03-12
        • 2017-11-23
        • 2019-06-27
        • 1970-01-01
        相关资源
        最近更新 更多