新手学习python写了个爬虫,为什么爬不下来数据

Jerry__du 2018-09-09 10:03:53
import requests
from lxml import etree
import re
import pymysql
import time

conn = pymysql.connect(host='localhost', user='root', passwd='admin', db='mydb',
port=3306, charset='UTF8')
cursor = conn.cursor()

headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1;WOW64)AppleWebKit/ 537.36 (KHTML,like Gecko) Chrome/56.0.2924.87 Safari/ 537.36'
}
def get_movie_url (url):
html = requests.get(url,headers=headers)
selector = etree.HTML(html.text)
movie_hrefs=selector.xpath('//div[@class="hd"]/a/@href')
for movie_href in movie_hrefs:
get_movie_info (movie_href)

def get_movie_info(url):
html = requests.get(url,headers=headers)
selector = etree.HTML(html.text)
try:
name = selector.xpath('//*[@id="content"]/h1/span[1]/text()')[0]
director = selector.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')[0]
actors = selector.xpath('//*[@id="info"]/span[3]/span[2]')[0]
actor = actors.xpath('string(.)')
style = re.findall ('<span property="v:genre">(.*?)</span>',html.text,re.S)[0]
country = re.findall ('<span class="p1">制片国家/地区:</span>(.*?)<br/>',html.text,re.S)[0]
release_time = re.findall ('上映日期:</span>.*?>(.*?)</span>',html.text,re.S)[0]
time = re.findall('上映日期:</span>.*?>(.*?)</span>', html.text, re.S)[0]
score = selector.xpath('//*[@id="interest_sect1"]/div[1]/div[2]/strong/text()')[0]
cursor.execute(
"insert into doubanmovie (name,director,actor,style,country,release_time,time,score)values(%s,%s,%s,%s,%s,%s,%s,%s)",
(str(name), str(director), str(actor),str(style), str(country), str(release_time),str (time), str(score)))

except IndexError:
pass

if __name__ == '_main_':
urls = [ 'https://movie.douban.com/top250?start{ }'.format(str(i))
for i in range(0,250,25)]
for url in urls:
get_movie_url(url)
time.sleep(2)
conn.commit()

...全文
1094 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
裸睡的雨 2018-09-29
  • 打赏
  • 举报
回复
main这是哪里写的代码,
嘻哈路过 2018-09-19
  • 打赏
  • 举报
回复
使用open去将爬去的内容存储在文件中,看一下就知道了
机智的小路飞 2018-09-11
  • 打赏
  • 举报
回复
你这个脚本在跑的时候根本没有运行
if __name == "__main__":
这样才对,你的没有__main__写成_main_了
oyljerry 2018-09-10
  • 打赏
  • 举报
回复
先看看你拿到什么数据了,是不是页面是ajax返回内容的

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧