所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地。
from bs4 import BeautifulSoup
import urllib.request
urllist = 'https://www.educity.cn/tiku/zt100110011004-1.html'
html = urllib.request.urlopen(urllist).read()
# 把html格式进行确定和纠正
soup = BeautifulSoup(html, 'html.parser')
# 找出tr标签中id属性为places_area__row的内容,如果把find改成findall函数则会把匹配所#有的内容显示出来,find函数只匹配第一次匹配的内容。
div = soup.find('div', attrs={'class': 'ecv2_tikucom_doTitle ecv2_marginbottom16'})
# td = tr.find('td', attrs={'class': 'w2p_fw'})
# 取出标签内容
area = div.text
print(area)
with open('444.txt', 'w', encoding='utf-8') as fp:
fp.write(area)
运行结果:














网友评论