1. from lxml import etree
    2. import requests
    3. HEADERS = {
    4. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
    5. 'Referer': 'http://www.dytt8.net/html/gndy/dyzz/list_23_1.html'
    6. }
    7. text = response.text
    8. #text=response.content.decode("gbk")
    9. html=etree.HTML(text)
    10. detail_urls=html.xpath("//table[@class='tbspan']//a/@href")

    json保存

    1. movies = []
    2. for li in all_li:
    3. movie = {}
    4. title = li['data-title']
    5. movie['title'] = title
    6. movies.append(movie)
    7. with open('douban.json','w',encoding='utf-8') as fp:
    8. json.dump(movies,fp,ensure_ascii=False)