鱼C论坛

 找回密码
 立即注册
查看: 1394|回复: 7

Python 爬虫求助

[复制链接]
发表于 2019-6-23 03:08:08 | 显示全部楼层 |阅读模式
10鱼币
Python 朋友的作业,我自己没学到爬虫! 想了半天没想懂
运行结果如图
用 url = 'http://www.baidu.com/s?wd=月亮虾饼怎么做&rn=50' 能正确获取编码和网站内容
而 url = 'https://www.baidu.com/s?wd=探店-发现世界.分享生活&rn=50' 却不行

完整代码如下
  1. import requests #python的http库
  2. import chardet  #用于获取网页的真正编码的第三方库
  3. import urllib3  #
  4. import os
  5. import jieba
  6. from collections import Counter
  7. from bs4 import BeautifulSoup #BeautifulSoup第三方库用于从HTML和XML文件中提取数据。

  8. urllib3.disable_warnings()

  9. rootPath = os.path.dirname(os.path.realpath(__file__))  #返回文件路径(返回__file__的真实路径)
  10. userDictPath = os.path.join(rootPath, 'jiebadic.csv')

  11. jieba.load_userdict(userDictPath)
  12. jieba.initialize()

  13. # 大佬看这里 ↓↓↓↓-----------------------------------

  14. #url = 'http://www.baidu.com/s?wd=月亮虾饼怎么做&rn=50'  #百度搜索的网站赋值给url
  15. url = 'https://www.baidu.com/s?wd=探店-发现世界.分享生活&rn=50'
  16. response = requests.get(url)  #找到 url

  17. htmlEncoded = response.content #把网站的内容赋值给htmlEncoded

  18. print(htmlEncoded)

  19. detectResult = chardet.detect(htmlEncoded) #检测htmlEncoded 返回一个字典键'encoding'的值就是编码

  20. print(detectResult)

  21. encoding = detectResult['encoding'] #把字典detectResult中 键'encoding'的‘值'赋给变量encoding ‘值’是自动检测到的 它可能是utf-8

  22. html = str(htmlEncoded, encoding)  #建立一个元组(网站内容,编码)

  23. soup = BeautifulSoup(html, 'html.parser') #通过BeautifulSoup来解析html变量中Html代码,使用html.parser解析器,因为BeautifulSoup还有其他几种解析器,如xml、html5lib等,所以这里必须确定使用什么解析器
  24. #print(soup)
  25. items = soup.select('h3 a') #获取html中所有h3标签下的a标签dom节点元素,所以我们使用了 'h3 a' 这个选择器

  26. allTitleStr = ''

  27. for item in items: #历遍items ???循环没有执行 items为空

  28.     resultRedirectUrl = item.attrs['href'] #网页中a标签的href属性值也就是a标签对应的锚链接

  29.     if 'http://' in resultRedirectUrl or \
  30.             'https://' in resultRedirectUrl:  #判断是不是一个真正的链接

  31.         itemHeadRes = requests.head(resultRedirectUrl, verify=False) #使用http协议中的head命令来访问百度加密连接head命令比get命令返回信息更加精简

  32.         itemUrl = itemHeadRes.headers['Location'] #通过headers的Location属性,便能得到解密后的url,我们把这个解密后的url网址存储到itemUrl变量中,以便后续读取页面Title使用。

  33.         try:    # 检测异常

  34.             itemRes = requests.get(itemUrl, verify=False) #找到网站itemUrl

  35.             if itemRes.status_code == 200: #if itemRes 的状态条码是 200

  36.                 itemHtmlEncoding = chardet.detect(itemRes.content)['encoding'] # --------------↓

  37.                 itemHtml = str(itemRes.content, itemHtmlEncoding, errors='ignore')

  38.                 itemSoup = BeautifulSoup(itemHtml, 'html.parser')  # -------------获取编码 同上面操作一样 通过BeautifulSoup来解析html变量中Html代码....

  39.                 if itemSoup.title is not None:   # --------------------↓  

  40.                     itemTitle = itemSoup.title.text.strip()

  41.                     #print(itemTitle)  # ?---- 没有打印

  42.                     allTitleStr += itemTitle+' ' # -------------------提取出html中的title
  43.         except:
  44.             continue #继续执行

  45. titleWords = [word for word in jieba.lcut(allTitleStr, cut_all=False) if len(word) > 1]

  46. titleWordsDic = dict(Counter(titleWords))

  47. titleWordsSortedList = sorted(titleWordsDic.items(), key=lambda x: x[1], reverse=True)

  48. for item in titleWordsSortedList:
  49.     print(item[0], ':', item[1])
复制代码



2.png
1.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-24 10:37:28 | 显示全部楼层
你把URL前面的https改成http试试看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-24 14:14:21 From FishC Mobile | 显示全部楼层
18307521742 发表于 2019-6-24 10:37
你把URL前面的https改成http试试看

不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-6-24 14:16:17 From FishC Mobile | 显示全部楼层
18307521742 发表于 2019-6-24 10:37
你把URL前面的https改成http试试看

试了好多方法都不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-24 14:29:15 From FishC Mobile | 显示全部楼层
13432477267 发表于 2019-6-24 14:16
试了好多方法都不行

关键看你最终想要啥数据,网页编码不是必须吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-24 15:28:16 | 显示全部楼层
headers都不加 真当百度你家开的?
原因就是你没有 请求头, 具体方法 你百度 python 爬虫 headers 看看怎么加 .
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

头像被屏蔽
发表于 2019-6-24 17:24:59 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-24 21:13:25 | 显示全部楼层
写的这么繁琐 !!!还有头部 user-agent
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-23 15:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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