鱼C论坛

 找回密码
 立即注册
查看: 2056|回复: 0

[技术交流] Python 爬取淘帖里的帖子标题(1)

[复制链接]
发表于 2020-3-30 11:43:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python 爬取淘帖里的帖子标题


  1. from requests import get
  2. from bs4 import BeautifulSoup as BS

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

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

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

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

  25. if __name__ == "__main__":
  26.     main()
复制代码


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

不要爬太多哦~

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 09:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表