注册 登录
编程论坛 Python论坛

新人求助 自制的爬虫无法爬取目标网站的点赞数

杨大雁 发布于 2020-03-02 02:17, 1210 次点击
python3.8  bs4  用的是atom  win10系统 自制的爬虫无法爬取目标网站的点赞数

from urllib import request,parse,error
import re
from bs4 import BeautifulSoup
page='2.html'
url='http://xiaohua.'+str(page)
headers={
'User-Agent':'Mozilla/4.0(compatible;MSIE 5.5;Windows NT)',
'host':'xiaohua.'
}
try:
    req=request.Request(url=url,headers=headers,method='POST')
    response=request.urlopen(req)
    html=response.read().decode('gbk')
    bs=BeautifulSoup(html,'html.parser')
    for tag in bs.find_all('em',class_='good-btn'):
        article=tag.find('span',class_='text')
        print(article)

except error.URLError as e:
    if hasattr(e,'code'):
        print(e.code)
    if hasattr(e,'reason'):
        print(e.reason)
0 回复
1