鱼C论坛

 找回密码
 立即注册
查看: 1944|回复: 10

[已解决]关于爬虫的问题

[复制链接]
发表于 2020-2-13 18:27:54 | 显示全部楼层 |阅读模式

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

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

x
这是原本实现效果:

1.png

这是我想要的实现效果:

2.png

就是在原本的实现效果后面增加帖子的链接,我搞来搞去还是高不出这种效果,大家帮我看看怎么才能实现这种效果,谢谢。

以下是代码:

  1. import requests
  2. import bs4
  3. import re

  4. def return_last(res):
  5.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  6.     content = soup.find("a", class_="last")
  7.     return content

  8. def write(content, num=1):
  9.     file = open("帖子.txt", "a", encoding="utf-8")
  10.    
  11.     for each in content:
  12.         file.write(str(num)+"."+each.text+"\n\n")
  13.         num += 1

  14.     file.write("-" * 50+"\n\n")

  15.     file.close()

  16.     return num

  17. def find_data(res):
  18.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  19.     content = soup.find_all(class_="s xst")

  20.     return content

  21. def open_url(url):
  22.     headers = {"user-agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
  23.     res = requests.get(url, headers=headers)

  24.     return res
  25.    
  26. def main():
  27.     # 用于清除文件里面的内容
  28.     with open("帖子.txt", "w", encoding="utf-8"):
  29.         pass
  30.    
  31.     url = input("请输入板块地址:")
  32.     page = int(input('请输入爬取的页数("0"表示全部):'))

  33.     if page == 0:
  34.         res = open_url(url)
  35.         content = return_last(res)
  36.         p = re.compile("\d")
  37.         page = int("".join(p.findall(content.text)))

  38.     num = 1                 # 用于翻页
  39.     subject_num = 1
  40.     new_url = url.split("-")
  41.     new_url = new_url[0] + "-" + new_url[1] + "-" + str(num) + ".html"

  42.     while num <= page:
  43.         print("正在爬取:", new_url, sep="")
  44.         res = open_url(new_url)
  45.         content = find_data(res)
  46.         subject_num = write(content, subject_num)
  47.         num += 1
  48.         new_url = url.split("-")
  49.         new_url = new_url[0] + "-" + new_url[1] + "-" + str(num) + ".html"

  50. if __name__ == "__main__":
  51.     main()
复制代码
最佳答案
2020-2-13 18:40:37
  1. import requests
  2. import bs4
  3. import re


  4. def return_last(res):
  5.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  6.     content = soup.find("a", class_="last")
  7.     return content


  8. def write(content, num=1):
  9.     file = open("帖子.txt", "a", encoding="utf-8")

  10.     for each in content:
  11.         file.write(str(num) + ". " + each.text + " -> " + each["href"] + "\n\n")
  12.         num += 1

  13.     file.write("-" * 50 + "\n\n")

  14.     file.close()

  15.     return num


  16. def find_data(res):
  17.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  18.     content = soup.find_all(class_="s xst")

  19.     return content


  20. def open_url(url):
  21.     headers = {
  22.         "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
  23.                       "(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
  24.     res = requests.get(url, headers=headers)

  25.     return res


  26. def main():
  27.     # 用于清除文件里面的内容
  28.     with open("帖子.txt", "w", encoding="utf-8"):
  29.         pass

  30.     url = input("请输入板块地址:")
  31.     page = int(input('请输入爬取的页数("0"表示全部):'))

  32.     if page == 0:
  33.         res = open_url(url)
  34.         content = return_last(res)
  35.         p = re.compile(r"\d")
  36.         page = int("".join(p.findall(content.text)))

  37.     num = 1  # 用于翻页
  38.     subject_num = 1
  39.     new_url = url.split("-")
  40.     new_url = new_url[0] + "-" + new_url[1] + "-" + str(num) + ".html"

  41.     while num <= page:
  42.         print("正在爬取:", new_url, sep="")
  43.         res = open_url(new_url)
  44.         content = find_data(res)
  45.         subject_num = write(content, subject_num)
  46.         num += 1
  47.         new_url = url.split("-")
  48.         new_url = new_url[0] + "-" + new_url[1] + "-" + str(num) + ".html"


  49. if __name__ == "__main__":
  50.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-13 18:32:36 | 显示全部楼层
本帖最后由 zltzlt 于 2020-2-13 18:36 编辑

直接用 BS 获取就行了,等会帮你写一个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 18:33:15 | 显示全部楼层
zltzlt 发表于 2020-2-13 18:32
直接用 BS 就行了,等会帮你写一个

好的,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 18:40:37 | 显示全部楼层    本楼为最佳答案   
  1. import requests
  2. import bs4
  3. import re


  4. def return_last(res):
  5.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  6.     content = soup.find("a", class_="last")
  7.     return content


  8. def write(content, num=1):
  9.     file = open("帖子.txt", "a", encoding="utf-8")

  10.     for each in content:
  11.         file.write(str(num) + ". " + each.text + " -> " + each["href"] + "\n\n")
  12.         num += 1

  13.     file.write("-" * 50 + "\n\n")

  14.     file.close()

  15.     return num


  16. def find_data(res):
  17.     soup = bs4.BeautifulSoup(res.text, "html.parser")
  18.     content = soup.find_all(class_="s xst")

  19.     return content


  20. def open_url(url):
  21.     headers = {
  22.         "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
  23.                       "(KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
  24.     res = requests.get(url, headers=headers)

  25.     return res


  26. def main():
  27.     # 用于清除文件里面的内容
  28.     with open("帖子.txt", "w", encoding="utf-8"):
  29.         pass

  30.     url = input("请输入板块地址:")
  31.     page = int(input('请输入爬取的页数("0"表示全部):'))

  32.     if page == 0:
  33.         res = open_url(url)
  34.         content = return_last(res)
  35.         p = re.compile(r"\d")
  36.         page = int("".join(p.findall(content.text)))

  37.     num = 1  # 用于翻页
  38.     subject_num = 1
  39.     new_url = url.split("-")
  40.     new_url = new_url[0] + "-" + new_url[1] + "-" + str(num) + ".html"

  41.     while num <= page:
  42.         print("正在爬取:", new_url, sep="")
  43.         res = open_url(new_url)
  44.         content = find_data(res)
  45.         subject_num = write(content, subject_num)
  46.         num += 1
  47.         new_url = url.split("-")
  48.         new_url = new_url[0] + "-" + new_url[1] + "-" + str(num) + ".html"


  49. if __name__ == "__main__":
  50.     main()
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
一个账号 + 5 + 5 非常感谢!

查看全部评分

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

使用道具 举报

 楼主| 发表于 2020-2-13 18:46:29 | 显示全部楼层


我好像找不到你改了哪里......你能不能告诉我你改了哪里?谢谢。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 18:46:54 | 显示全部楼层
一个账号 发表于 2020-2-13 18:46
我好像找不到你改了哪里......你能不能告诉我你改了哪里?谢谢。

只改了第 16 行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 18:51:07 | 显示全部楼层

为什么你又添加了一个 user-agent
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 18:51:45 | 显示全部楼层
一个账号 发表于 2020-2-13 18:51
为什么你又添加了一个 user-agent

User-Agent 一行太长不美观
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 18:54:03 | 显示全部楼层
zltzlt 发表于 2020-2-13 18:51
User-Agent 一行太长不美观

我知道了,你把那两个分开写了,原来还可以这样,谢谢。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 18:59:32 | 显示全部楼层
zltzlt 发表于 2020-2-13 18:51
User-Agent 一行太长不美观

我发现这个程序其他论坛也可以用,如卡饭。

因为那个 class 没变。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-13 19:00:13 | 显示全部楼层
一个账号 发表于 2020-2-13 18:59
我发现这个程序其他论坛也可以用,如卡饭。

因为那个 class 没变。

哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-3 08:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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