weixin_51848762 2021-08-12 15:48 采纳率: 50%
浏览 31
已结题

为什么爬取CSDN热搜存MySQL为空?如何解决


import requests
import pymysql
from lxml import etree

conn = pymysql.connect(host='127.0.0.1', user='root', password='*****', port=3306, database='热搜爬取')

cursor = conn.cursor()


headers={
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67"
}
url = 'https://blog.csdn.net/rank/list'

reponse = requests.get(url, headers=headers)   # reponse
reponse.encoding = 'utf-8'
html = etree.HTML(reponse.text)

title = html.xpath('//div[@class="hosetitem-title"]/a/text()')
url1 =html.xpath('//div[@class="hosetitem-title"]/a/@href')


for n in range(len(title)):
    print(url1[n])
    sql = "insert into CSDN(排行,标题,网址) values('%d','%s','%s')"%(n+1,title[n],url1[n])
    #print(title[n])
    #print(url[n])
    cursor.execute(sql)
    conn.commit()

cursor.close()
# 关闭连接
conn.close()

  • 写回答

1条回答 默认 最新

  • python收藏家 2021-08-12 16:14
    关注

    没有报错? 还是爬取的结果为空? 或者数据库表存在?字段类型对应?首先看是爬取问题还是数据库存储问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月20日
  • 已采纳回答 8月12日
  • 创建了问题 8月12日

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现