会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
Python 全系列/第十五阶段:Python 爬虫开发/scrapy 框架高级 1021楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 1022楼

image.png

image.png

image.png

import requests
from PIL import Image
from fake_useragent import UserAgent
from urllib3.exceptions import InsecureRequestWarning
import urllib3
import json
urllib3.disable_warnings(InsecureRequestWarning)


def login():
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36"
    }
    session = requests.session()
    img_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image?login_site=E&module=login&rand=sjrand'
    resp_img = session.get(img_url, headers=headers, verify=False)
    with open("yzm.png", "wb") as f:
        f.write(resp_img.content)
    a = Image.open("yzm.png")
    a.show()
    a.close()
    solution = input("请输入图片对应的号码,多张图片以,号隔开:")
    yzh_list = solution.split(",")
    zb_list = ['45,45', '119,49', '188,45', '269,42', '46,121', '101,120', '187,113', '259,112']
    zb_list1 = []
    for i in yzh_list:
        zb_list1.append(zb_list[int(i) - 1])
    yzm_1 = ",".join(zb_list1)
    yz_url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
    parameters = {
        'answer': yzm_1,
        'login_site': 'E',
        'rand': 'sjrand'
    }
    resp_check = session.get(yz_url, headers=headers, params=parameters, verify=False)
    code = json.loads(resp_check.text)
    # print(code)
    if code['result_code'] == '4':
        print("验证码校验成功")
        login_url = 'https://kyfw.12306.cn/passport/web/login'
        formdate = {
            "username": "143049962@qq.com",
            "password": "130342Abc",
            "appid": "otn"
        }
        resp_login = session.post(login_url, headers=headers, data=formdate, verify=False)
        code = json.loads(resp_login.content)


if __name__ == '__main__':
    login()

老师,我用网页登录12306,使用fiddler抓包可以得到json的响应信息,但是在pycharm中却不能用loads转换,帮忙看看是啥原因

Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 1023楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 1024楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 1025楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 1026楼
Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 1028楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 1029楼

当我使用这个正则能够匹配出数据,但是不能对应文字

re.findall(r'<div class="qiushi" id="article[2-5][0-9]">',resp.text

image.png

但是我一旦加上这些就会出现匹配不成功的混乱现象

image.png

求老师帮我找一下问题在哪里,这是网页部分源码:

['我:姐,家里还有黄瓜吗?<br/>姐:没有。问有没有黄瓜干什么?<br/>我:有用。<br/>说完我就看到老姐不安分的笑了,然后就出门了,我慌忙开门问她去干什么,她说:我去挑两根好黄瓜来。<br/>姐、不是,我、我只是想用它来贴脸.....擦</div><div class="qiushi_counts " id="qiushi_counts_22"><div class="counts"><div id="score-22" class="score" style="display:inline"><p class="vote"><a href="javascript:vote2(22,1)" id="vote-up-22" class="vote"><strong>支持(<span id="up-22">6</span>)</strong></a>&nbsp;|&nbsp;<a href="javascript:vote2(22,-1)" id="vote-dn-22" class="down"><strong>反对(<span id="dn-22">-2</span>)</strong></a>&nbsp;|&nbsp;<a class="qiushi_comments" id="c-22" href="http://qiushi.92game.net/e/zxfcode/92game_commentlist.php?id=22&classid=1&page=1"><strong>评论(0)</strong></a></p></div></div></div><br><p class="user"><a href="/users/?uid=2"><img src="http://qiushi.92game.net/e/zxfcode/92game_avatar.php?uid=2"/>糗事小编</a> </p><div class="qiushi" id="article23">就刚刚我在摸老婆mm,老婆让我别摸了我不听,她就用足了劲冲我手打了上来,只听啊得一声我的手躲开了。</div>

Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 1031楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础(旧) 1032楼

我准备爬取https://m.lread.net的一本小说,这小说有时候 一个章节分了两页html。我如何将第二页的内容与前一页内容合并,保存到csv文件里?

image.png

#pipelines.py
import csv
class RuanyunPipeline(object):
    def __init__(self):
        self.filename= open('douluodalu.csv','w',newline="",encoding='utf-8')
        self.writer=csv.writer(self.filename)
        self.writer.writerow(['title','url_next','content'])
    def process_item(self, item, spider):
        if '-' not in item['url_now']:
            self.writer.writerow([item['title'],item['url_now'],item['content']])
        else :
            self.writer.writerow([item['title'],item['url_now'],item['content']])
        return item
    def close_spider(self,spider):
        if self.filename:
            self.filename.close()

ruanyun.zip


Python 全系列/第十五阶段:Python 爬虫开发/移动端爬虫开发- 1033楼
Python 全系列/第十五阶段:Python 爬虫开发/scrapy框架使用(旧) 1034楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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