鱼C论坛

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

[技术交流] 新浪新闻首页新闻标题和链接

[复制链接]
发表于 2021-8-27 17:09:45 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 wcq15759797758 于 2021-8-27 17:12 编辑

很简单的爬虫
  1. import requests
  2. import cchardet
  3. import traceback
  4. from lxml import etree

  5. def downloader(url, timeout=10, headers=None, debug=False, binary=False):
  6.     _headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
  7.     redirected_url = url
  8.     if headers:
  9.         _headers = headers
  10.     try:
  11.         r = requests.get(url, headers=_headers, timeout=timeout)
  12.         if binary:
  13.             html = r.content
  14.         else:
  15.             encoding = cchardet.detect(r.content)['encoding']
  16.             html = r.content.decode(encoding)
  17.         status = r.status_code
  18.         redirected_url = r.url
  19.     except:
  20.         if debug:
  21.             traceback.print_exc()
  22.         msg = 'failed download: {}'.format(url)
  23.         print(msg)
  24.         if binary:
  25.             html = b''
  26.         else:
  27.             html = ''
  28.         status = 0
  29.     return title(html)

  30. def title(html):
  31.     title_html = etree.HTML(html)
  32.     titles = title_html.xpath('//a[@target="_blank"]')
  33.     for title in titles:
  34.         item = {}
  35.         tit = title.xpath('./text()')
  36.         urls = title.xpath('./@href')
  37.         item['title'] = str(processing(tit))
  38.         item['url'] = str(processing(urls))
  39.         if len(item['title']) > 4 :
  40.                 print(item)

  41. def processing(strs):
  42.     s = ''  # 定义保存内容的字符串
  43.     for n in strs:
  44.         n = ''.join(n.split())  # 去除空字符
  45.         s = s + n  # 拼接字符串
  46.     return s      # 返回拼接后的字符串
  47.         

  48. if __name__ == '__main__':
  49.     url = 'https://news.sina.com.cn/'
  50.     downloader(url)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 17:39:20 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-27 17:57:50 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-28 10:30:10 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-28 10:43:07 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-28 11:35:53 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-28 11:51:23 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-28 14:34:21 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-29 09:27:52 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-29 09:32:04 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

发表于 2021-8-29 12:13:26 | 显示全部楼层

回帖奖励 +1 鱼币

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 09:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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