鱼C论坛

 找回密码
 立即注册
查看: 2477|回复: 3

[已解决]爬取网站标题,作者信息,简介,文章内容

[复制链接]
发表于 2021-1-27 19:55:35 | 显示全部楼层 |阅读模式

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

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

x

  1. from urllib.request import urlopen, Request
  2. from bs4 import BeautifulSoup  # 网页解析  获取数据
  3. import re  # 正则表达式 进行文字匹配
  4. import random

  5. url = 'https://cybernews.com/news/'
  6. headers = {'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
  7. ret = Request(url)
  8. html = urlopen(ret)
  9. bs = BeautifulSoup(html, 'html.parser')

  10. names = bs.find_all("h3",{"class":"jeg_post_title"})
  11. for name in names:
  12.     name = name.get_text()
  13.     print(name)
  14. neirongs = bs.find_all("div",{"class":"jeg_post_meta"})
  15. for neirong in neirongs:
  16.     neirong = neirong.get_text()
  17.     print(neirong)
  18. contents = bs.find_all("div",{"class":"jeg_post_excerpt"})
  19. for content in contents:
  20.     content = content.get_text()
  21.     print(content)

复制代码

刚接触爬虫,大佬们能看下问题所在吗,以及爬取文章内容这个不太会
最佳答案
2021-2-1 20:02:21
本帖最后由 笨鸟学飞 于 2021-2-1 20:04 编辑
  1. import requests
  2. from lxml import etree


  3. if __name__ == '__main__':
  4.     url = 'https://cybernews.com/news/'
  5.     headers = {
  6.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
  7.     res = requests.get(url, headers=headers)
  8.     res.encoding = res.apparent_encoding
  9.     tree = etree.HTML(res.text)
  10.     # 标题
  11.     titles = tree.xpath('//div[@class="jeg_posts jeg_load_more_flag"]/article/h3/a/text()')
  12.     # 作者
  13.     authors = tree.xpath('//div[@class="jeg_meta_author"]/a/text()')
  14.     # 简介
  15.     excerpts = tree.xpath('//div[@class="jeg_post_excerpt"]/p/text()')
  16.     # 详情页面url
  17.     text_urls = tree.xpath('//div[@class="jeg_posts jeg_load_more_flag"]/article/h3/a/@href')
  18.     with open('./1.txt', 'w', encoding='utf-8') as fp:
  19.         for title,anthor,excerpt,text_url in zip(titles, authors, excerpts, text_urls):
  20.             fp.write(title+'\nby:'+anthor+'\n'+excerpt+'\n'+text_url+'\n\n')
复制代码

==============
你的headers写法是错误的,少了冒号。。。
详情页面的数据爬取自己写吧,已经到了这个地步了相信难不倒你了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-28 17:48:56 | 显示全部楼层
本帖最后由 YunGuo 于 2021-1-28 17:52 编辑

定义了headers不添加进去?headers是一个集合?响应结果不读取?建议基础多学学再动手
这里改下
  1. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'}
  2. ret = Request(url, headers=headers)
  3. html = urlopen(ret)
  4. bs = BeautifulSoup(html.read(), 'html.parser')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-29 14:27:18 | 显示全部楼层
这三个内容不用分开使用三次循环 太麻烦了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-1 20:02:21 | 显示全部楼层    本楼为最佳答案   
本帖最后由 笨鸟学飞 于 2021-2-1 20:04 编辑
  1. import requests
  2. from lxml import etree


  3. if __name__ == '__main__':
  4.     url = 'https://cybernews.com/news/'
  5.     headers = {
  6.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
  7.     res = requests.get(url, headers=headers)
  8.     res.encoding = res.apparent_encoding
  9.     tree = etree.HTML(res.text)
  10.     # 标题
  11.     titles = tree.xpath('//div[@class="jeg_posts jeg_load_more_flag"]/article/h3/a/text()')
  12.     # 作者
  13.     authors = tree.xpath('//div[@class="jeg_meta_author"]/a/text()')
  14.     # 简介
  15.     excerpts = tree.xpath('//div[@class="jeg_post_excerpt"]/p/text()')
  16.     # 详情页面url
  17.     text_urls = tree.xpath('//div[@class="jeg_posts jeg_load_more_flag"]/article/h3/a/@href')
  18.     with open('./1.txt', 'w', encoding='utf-8') as fp:
  19.         for title,anthor,excerpt,text_url in zip(titles, authors, excerpts, text_urls):
  20.             fp.write(title+'\nby:'+anthor+'\n'+excerpt+'\n'+text_url+'\n\n')
复制代码

==============
你的headers写法是错误的,少了冒号。。。
详情页面的数据爬取自己写吧,已经到了这个地步了相信难不倒你了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 11:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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