qiuyouzhi 发表于 2020-3-30 11:43:27

Python 爬取淘帖里的帖子标题(1)

Python 爬取淘帖里的帖子标题

from requests import get
from bs4 import BeautifulSoup as BS

def get_Tz(res):
    soup = BS(res.text, "html.parser")
    targ = soup.find_all("a",class_="xst", target="_blank")
    for each in targ:
      print(each.text)
      #print(each['title'])
      print(each['href'])

def get_Tz_write(res, f):
    soup = BS(res.text, "html.parser")
    targ = soup.find_all("a",class_="xst", target="_blank")
    for each in targ:
      f.write(each.text)
      f.write(each['href'])
      f.write('\n')

def main():
    n = int(input("请输入要下载的次数:"))
    f = open("淘帖.txt", "w")

    for i in range(1,n):
      url = "https://fishc.com.cn/forum.php?mod=collection&action=view&ctid=%d" % i
      res = get(url)
      get_Tz_write(res, f)
    f.close()

if __name__ == "__main__":
    main()

有时候会报错,是正常现象,重新运行就好

不要爬太多哦~
页: [1]
查看完整版本: Python 爬取淘帖里的帖子标题(1)