简体   繁体   English

使用 Beautiful soup 抓取 web 页面

[英]Scraping a web page using Beautiful soup

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
url = input('Enter -')
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html,'html.parser')

tags = soup('a')
for tag in tags:
    print(tag.get('herf',None))

I used this link to test my code http://www.dr-chuck.com/page1.htm我使用此链接测试我的代码http://www.dr-chuck.com/page1.htm

The output is: NONE output 是: NONE

the output should be this link http://www.dr-chuck.com/page2.htm output 应该是这个链接http://www.dr-chuck.com/page2.htm

Simple typo, there.简单的错字,那里。

Change 'herf'to 'href'in tags.get在 tags.get 中将 'herf' 更改为 'href'

  import urllib.request, urllib.parse, urllib.error
    from bs4 import BeautifulSoup
    url = input('Enter -')
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html,'html.parser')

    tags = soup('a')
    for tag in tags:
        print(tag.get('href',None))

outputs输出

#http://www.dr-chuck.com/page2.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM