新人一枚,学了几天PYTHON爬虫,爬了一下猫眼~
没有写文件处理的方法,还是感觉XPATH做爬虫最简单import requests
from requests.exceptions import RequestException
from lxml import etree
def get_one_page(url):
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'}
response = requests.get(url , headers = headers)
try:
if response.status_code == 200:
return response.text
return None
except RequestException:
print('You can not do it~')
def parse_one_page(html):
movie_list = etree.HTML(html).xpath('//dl[@class="board-wrapper"]/dd')
for movie in movie_list:
yield{
'index' : movie.xpath('./i/text()'),
'name' : movie.xpath('.//div[@class="movie-item-info"]//a/text()'),
'star' : movie.xpath('.//div[@class="movie-item-info"]//p[@class="star"]/text()').replace('\n',''),
'releasetime' : movie.xpath('.//div[@class="movie-item-info"]//p[@class="releasetime"]/text()'),
}
def main(offset):
url = 'http://maoyan.com/board/4?offset=' + str(offset)
html = get_one_page(url)
dy = parse_one_page(html)
for i in dy:
print(i)
if __name__ == '__main__':
for i in range(10):
offset = i * 10
main(offset)
你们新手怕不是都是有语言底子的,看了下次连新手都不能叫了 hugangtingQ 发表于 2018-11-21 11:15
你们新手怕不是都是有语言底子的,看了下次连新手都不能叫了
肯定是python的新手啊,有底子学得快,当你学了C++,Java。再来看python就很容易了
新手一枚,也请大神多指点~
感觉star这个主演字段还能处理一下,求大家好的建议~{:1_918:} 加油,共同学习 共勉,同学中!!! 学习了,...................... 学习......... 共同学习 学习了~也是基于java的语言?