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

代码在这:

# -*- coding: utf-8 -*-
import scrapy


class BiqukuSpider(scrapy.Spider):
    name = 'biquku'
    allowed_domains = ['biquku.com']
    start_urls = ['https://www.biquku.co/5023/3234391.html']

    def parse(self, response):
        title=response.xpath('//h1/text()').extract_first()
        content=response.xpath('string(//div[@id="content"])').extract_first().strip()
        next_url=response.xpath('//a[@id="pager_next"]/@href').extract_first()

        yield {
            'title':title,
            'content':content
        }
        yield scrapy.Request(response.urljoin(next_url),callback=self.parse)#response.urljoin代表自动补齐url

下面是pipelines的

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class XiaoshuoPipeline(object):
    def open_spider(self,spider):
        self.filename=open('quanzhifashi.txt','w',encoding='utf-8')

    def process_item(self, item, spider):
        print(item)
        info=item['title']+'\n'+item['content']+'\n'
        self.filename.write(info)
        return item

    def close_spider(self,spider):
        self.filename.close()

settings的三个设置我也修改了,但是只能爬到一章。。。。。

Python 全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 526楼

untitled8.zip

老师我找了好久也不知道为啥报错,他没有改名成功,但是图片能成功下载

image.png


Python 全系列/第十六阶段:Python 爬虫开发/scrapy 框架高级 527楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 528楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy框架使用 529楼


1.png

为什么装了pyquery没有pyQuery啊


Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础 530楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 532楼

d_xiaoshuo.zip

老师我的title返回来是空值,nonetype,内容是好着的,但是目前只有第一章的

Python 全系列/第十六阶段:Python 爬虫开发/移动端爬虫开发- 533楼

image.png

Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础 534楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 535楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫反反爬 536楼

老师,下面这个代码是为了下载搜狐登录界面的验证码图片,但是我换了几种方式发送请求,打印resp.text都是得到:

{"body":"","message":"Bad Request","status":400},但是打印的验证码图片链接又可以访问,这是为啥呢?

具体代码如下:

import execjs
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
    'Referer': 'https://sohu.com/'}

# 获得验证码链接中的pagetoken和random参数
def get_form_data():
    js_callback = """var randomName = function(prefix) {
                nonce = (new Date).getTime();
                return "passport403" + "_" + prefix + nonce++
            }"""

    ctx = execjs.compile(js_callback)
   
    random_ = ctx.call('randomName', 'sdk')

    js_pagetoken = """
        var pagetoken = (new Date).getTime()+1
        return pagetoken
    """
    ctx_page = execjs.compile(js_pagetoken)
    pagetoken = str(ctx_page.call(js_pagetoken))
    return (pagetoken, random_)


pagetoken, random_ = get_form_data()

url = f"https://v4.passport.sohu.com/i/captcha/picture?pagetoken={pagetoken}&random={random_}"
# 发送请求
resp = requests.get(url,headers=headers)
print(resp.text)
# {"body":"","message":"Bad Request","status":400}
print(url)


Python 全系列/第十六阶段:Python 爬虫开发/动态数据抓取 538楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy 框架高级 539楼
Python 全系列/第十六阶段:Python 爬虫开发/爬虫基础(旧) 540楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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