Python 爬取投诉建议专区的帖子
本帖最后由 一个账号 于 2020-3-30 17:04 编辑Python 爬取投诉建议专区的帖子
from requests import get
from bs4 import BeautifulSoup as BS
def open_url(url):
# headers 自己填
res = get(url,headers = headers)
return res
def get_T(res, f):
soup = BS(res.text, "html.parser")
target = soup.find_all("a", onclick="atarget(this)", class_="s xst")
for each in target:
f.write(each.text)
f.write(' ')
f.write(each['href'])
f.write('\n')
def main():
f = open("投诉建议专区.txt", 'w', encoding = "gbk")
for i in range(1, 21):
try:
url = 'https://fishc.com.cn/forum-26-%d.html' % i
res = open_url(url)
get_T(res, f)
except UnicodeEncodeError as reason:
print("出错啦!错误原因: ", reason)
f.close()
if __name__ == "__main__":
main()
headers自己填,不然爬不到(加上Cookie)。
怪不得论坛这么卡,原来是你!!!!!!! 乘号 发表于 2020-3-30 17:39
怪不得论坛这么卡,原来是你!!!!!!!
得了吧
我每次测试最多爬5页
做人,说话要有证据 乘号 发表于 2020-3-30 17:39
怪不得论坛这么卡,原来是你!!!!!!!
只爬5页 乘号 发表于 2020-3-30 17:43
只爬5页
5页够少了
页:
[1]