python 使用BeautifulSoup爬网浏览

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用BeautifulSoup爬网浏览相关的知识,希望对你有一定的参考价值。

# To run this, you can install BeautifulSoup
# https://pypi.python.org/pypi/beautifulsoup4

# Or download the file
# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file


from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = "http://py4e-data.dr-chuck.net/known_by_Danish.html"
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
name_list = ["Danish"]

# Retrieve all of the anchor tags
tags = soup('a')
name_list.append(tags[17].contents[0])


for number in range(6):
    url = (tags[17].get('href', None))
    html = urlopen(url, context=ctx).read()
    soup = BeautifulSoup(html, "html.parser")
    tags = soup('a')
    name_list.append(tags[17].contents[0])


print(name_list)

# for tag in tags:
#     # Look at the parts of a tag
#     # print('TAG:', tag)
#     print('URL:', tag.get('href', None))
#     print('Contents:', tag.contents[0])
#     # print('Attrs:', tag.attrs)

以上是关于python 使用BeautifulSoup爬网浏览的主要内容,如果未能解决你的问题,请参考以下文章

使用beautifulsoup 和selenium webdriver 需要帮助Web 抓取表

Docker最全教程之Python爬网实战(二十一)

Python爬网获取全国各地律师电话号

python MongoDB支持的宽度优先的Web爬网程序框架。

python 分析常见爬网索引 - http://index.commoncrawl.org/

python 分析常见爬网索引 - http://index.commoncrawl.org/