鱼C论坛

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

爬虫学习贴,大神请进解惑。本网页将不定期更新升级爬虫

[复制链接]
发表于 2017-2-7 22:47:08 | 显示全部楼层 |阅读模式

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

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

x
该贴用来学习爬虫。初步编了一个爬虫,用来爬电影天堂中欧美电影的网址。已能实现在第一页里爬取电影名称及网页网址,放在一个csv文件里。
疑问是beautifulsoup对象的编码存在问题,网页是gb2312编码,用了gbk,但内容仍显示错码,请大神解决。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-2-7 22:47:48 | 显示全部楼层
本帖最后由 易水寒楠 于 2017-2-7 22:58 编辑

以下为代码:
  1. #-*- coding:utf8 -*-
  2. import requests
  3. import re
  4. from bs4 import BeautifulSoup
  5. import csv
  6. def getHtml(url):                  #获取网址并形成requests对象并编码
  7.     res=requests.get(url)
  8.     html=res.text.encode('utf-8',errors='ignore')
  9.     return html
  10. def getPageUrl(html):             #解析网页,获取网址对象
  11.     bs0bj=BeautifulSoup(html,from_encoding='gbk')
  12.     reg=re.compile(r'/html/gndy/\w{4}/\d{8}/\d{4,10}.html')
  13.     pages=bs0bj.findAll('a', {'href':reg})
  14.     return pages

  15. if __name__ == '__main__':
  16.     url='http://www.ygdy8.net/html/gndy/oumei/index.html'
  17.     html=getHtml(url)
  18.     pages=getPageUrl(html)
  19.     csvFile=open('I:/编程学习/spider/movie.csv','w+')
  20.     sheet=csv.writer(csvFile)
  21.     sheet.writerow(('电影名称','电影介绍及下载网页网址'))
  22.     preurl='http://www.ygdy8.net/html/gndy'
  23.     for page in pages:
  24.         sheet.writerow((page.get_text(),preurl+page['href']))
  25.     csvFile.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-7 22:49:36 | 显示全部楼层
以下为运行后输出:
C:\Users\chennan\AppData\Local\Programs\Python\Python35-32\python.exe I:\编程学习\spider\.idea\dytt8-movie.py
C:\Users\chennan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\bs4\__init__.py:181: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.

The code that caused this warning is on line 20 of the file I:\编程学习\spider\.idea\dytt8-movie.py. To get rid of this warning, change code that looks like this:

BeautifulSoup([your markup])

to this:

BeautifulSoup([your markup], "html.parser")

  markup_type=markup_type))

进程已结束,退出代码0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-7 22:56:04 | 显示全部楼层
保存的文件,请注意红圈处乱码
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-7 22:57:24 | 显示全部楼层
本帖最后由 易水寒楠 于 2017-2-7 22:59 编辑

请大神指导解决。我一定好好学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-7 23:23:57 | 显示全部楼层
这位哥哥,帖子发错地方啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-7 23:47:15 | 显示全部楼层
爬虫升级,现在已可以爬到所有欧美电影的网页网址。单单利用其网页的序号结构迭代实现。
  1. __author__ = 'chennan'
  2. #-*- coding:utf8 -*-
  3. import requests
  4. import re
  5. from bs4 import BeautifulSoup
  6. import csv
  7. def getHtml(url):                  #获取网址并形成requests对象并编码
  8.     res=requests.get(url)
  9.     html=res.text.encode('utf-8',errors='ignore')
  10.     return html
  11. def getPageUrl(html):             #解析网页,获取网址对象
  12.     bs0bj=BeautifulSoup(html,from_encoding='gbk')
  13.     reg=re.compile(r'/html/gndy/\w{4}/\d{8}/\d{4,10}.html')
  14.     pages=bs0bj.findAll('a', {'href':reg})
  15.     return pages

  16. if __name__ == '__main__':
  17.     preurl='http://www.ygdy8.net/html/gndy/oumei/'
  18.     csvFile=open('I:/编程学习/spider/movie.csv','w+')
  19.     sheet=csv.writer(csvFile)
  20.     sheet.writerow(('电影名称','电影介绍及下载网页网址'))
  21.     for count in range(1,160):
  22.         x=count
  23.         urls=preurl+'list_7_'+str(x)+'.html' #每一页网页都是http://www.ygdy8.net/html/gndy/oumei/list_7_N.html形式,N是1~159数字
  24.         html=getHtml(urls)
  25.         pages=getPageUrl(html)
  26.         prepageurl='http://www.ygdy8.net/html/gndy'
  27.         for page in pages:
  28.             sheet.writerow((page.get_text(),prepageurl+page['href']))
  29.     csvFile.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-8 00:31:10 From FishC Mobile | 显示全部楼层
零度非安全 发表于 2017-2-7 23:23
这位哥哥,帖子发错地方啦

能帮我换个地方吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-31 21:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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