会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134310个问题

爬取一次之后就不能爬取了,建议爬取的时候在请求头里增加cookie属性,

def text_bs4():
    url = "https://www.maoyan.com/films"
    headers = {'User-Agent': UserAgent().firefox,
               "Cookie": 'uuid_n_v=v1; uuid=8236EB905E8611EEB2C88F0E1D778D2976602F68457842108D4B5999AAA9AC1B; '
                         '_csrf=95caf440d93adc2cf97b85c916a33ea5d547aa09c22e33e4109c0771222dc415; '
                         '_lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; '
                         '_lxsdk_cuid=18adf587d66c8-037b0eb95fbfca-26031e51-1bcab9-18adf587d66c8; '
                         '_lxsdk=8236EB905E8611EEB2C88F0E1D778D2976602F68457842108D4B5999AAA9AC1B; '
                         'Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1695964233; '
                         'Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1695965039; '
                         '__mta=142327309.1695964233411.1695964233411.1695965039712.2; '
                         '_lxsdk_s=18adf587d67-d26-b7b-f21%7C%7C4'}
    resp = requests.get(url, headers=headers)

    soup = BeautifulSoup(resp.text, 'lxml')
    names = [div.text.strip('\n') for div in soup.select("div.channel-detail.movie-item-title")]
    score = [div.text for div in soup.select("div.channel-detail.channel-detail-orange")]
    with open('猫眼bs4.txt', 'w', encoding='utf-8') as f:
        for n, s in zip(names, score):
            print(n, s ,file=f)


Python全系列/第十六阶段:Python 爬虫开发/爬虫基础 556楼
Python全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 557楼

from fake_useragent import UserAgent
import re
import requests
from bs4 import BeautifulSoup
from time import sleep

def get_html(url):
    '''
    :param url: 要爬取的地址
    :return: 返回html
    '''
    headers = {"User_Agent": UserAgent().random}
    resp = requests.get(url,headers=headers)

    #status_code   返回状态码
    if resp.status_code == 200:
        resp.encoding='utf-8'
        return resp.text
    else:
        return None

def parse_list(html):
    '''
    :param html: 传递进来有一个电影列表的的html
    :return: 返回一个电影的url
    '''

    soup = BeautifulSoup(html,'lxml')
    a_list = soup.select(".book-img-box > a")
    list_url = []
    for a in a_list:
        list_url.append(a.get('href'))
    # 解决验证CA
    # ssl._create_default_https_context = ssl._create_unverified_context
    list_url = ['https:{}'.format(url)for url in list_url]
    return list_url

def parse_index(html):
    '''
    :param html: 传递一个有电影信息的html
    :return: 已经提取好的电影信息
    '''
    soup = BeautifulSoup(html,'lxml')
    name = soup.find('h1','em')
    book = soup.find_all("a", class_="writer")[0].text
    return {'作者':name,'书名':book}

def main():
    num = int(input("请输入要获取多少页:"))
    for page in range(num):
        url = 'https://www.qidian.com/all?&page={}'.format(page+1)
        list_html = get_html(url)
        list_url = parse_list(list_html)
        for url in list_url:
            info_html = get_html(url)
            move = parse_index(info_html)
            print(move)

if __name__ == '__main__':
    main()


老师,我这个匹配到h1下面的em标签啊,我网上也查了,也用calss匹配过。最后返回要么空,要么是错误的。

image.png


image.png

Python全系列/第十六阶段:Python 爬虫开发/爬虫反反爬- 559楼
Python全系列/第十六阶段:Python 爬虫开发/爬虫基础 561楼
Python全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 562楼
Python全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 563楼
Python全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 565楼
Python全系列/第十六阶段:Python 爬虫开发/爬虫基础(旧) 566楼
Python全系列/第十六阶段:Python 爬虫开发/scrapy框架使用(旧) 568楼
Python全系列/第十六阶段:Python 爬虫开发/爬虫基础(旧) 570楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637