代理IP,爬微信公众号的网页

先通过西刺制作IP代理池,然后伪造USERAGENT 为安卓机或者
苹果机,通过筛选出的可用IP,登陆目标网页,本意是通过电脑抓
包投票的POST 的data和PARA来构造相应项。
但狗腾讯对PC端的限制,PC端只能浏览网页不能投票,不能抓取到
POST传递信息,本爬虫就只能做到构建代理池,通过代理池和安卓
或苹果的useragent访问网站,若有需要可爬取内容,但无法模拟
手机投票或者刷票


附上PYTHON代码,用到的requests包
# coding=utf-8
import urllib2
import random
import time
import requests
import re

class JB():
    def __init__(self):
        self.pro_g = []
        print('赋值完成')
        self.n1()
        self.n2()


    def n1(self):
        a1={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
        htm=requests.get('http://www.xicidaili.com/nt',headers=a1)
        pattern = re.compile('<td class="country">.*? alt="Cn" /></td>.*?<td>(.*?)</td>.*?<td>(.*?)</td>',re.S)

        items = re.findall(pattern, htm.text)
        for item in items:
            X=(item[0]+':'+item[1])
            print X
            self.pro_g.append(X)
        print ('爬取IP完成个数:')
        print(len(self.pro_g))
        return self.pro_g


    def n2(self):
        print self.pro_g
        a=0
        a1={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64)'}
        sx={'User-Agent':'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255'}
        for a in range(0, 80):
            proxies_l = {'http': self.pro_g[a],

                 }
            print(proxies_l['http'])

            try:
                req=requests.get('http://httpbin.org/ip',headers=a1,proxies=proxies_l)
                print('可以用的IP')
                print (req.text)
                req2=requests.get('http://mp.weixin.qq.com/s/ep30umOyUNS03IvFSj7msg',headers=sx,proxies=proxies_l)
                req2=req2.text
                print('得到PAGE')
                print(req2)


            except:
                print('no proxies')
            sleep_time=random.randint(1, 3)
            time.sleep(sleep_time)
            print('Wait%ds'%sleep_time)

spider=JB()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬虫是一种自动抓取网页数据的程序,可以用于获取微信公众号的内容信息。下面是一个简单的Python爬虫示例,使用了`requests`库来发送HTTP请求并解析HTML内容,以及使用了`BeautifulSoup`库来进行HTML内容的解析: ```python import requests from bs4 import BeautifulSoup def get_wechat_article(url): # 发送GET请求到指定URL获取文章页面 response = requests.get(url) # 检查请求是否成功 if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') # 解析文章标题、作者、发布日期等信息 title = soup.find('title').text.strip() author = soup.find(id="js_content").find_previous("h2", class_="rich_media_title").text.strip() date = soup.find(id="js_content").find_next_sibling("span").text.strip() # 解析文章内容 article_text = "" for paragraph in soup.find_all("p"): article_text += paragraph.text.strip() + "\n\n" return {'title': title, 'author': author, 'date': date, 'content': article_text} else: print(f"Request failed with status code {response.status_code}") return None # 使用示例 url = "https://mp.weixin.qq.com/s/YsJZxXjwO7oBzRyvLk986A" # 微信公众号文章链接 article_info = get_wechat_article(url) if article_info is not None: print(f"Title: {article_info['title']}\nAuthor: {article_info['author']}\nDate: {article_info['date']}") print("\nContent:\n") print(article_info['content']) else: print("Failed to fetch the article.") ``` 请注意,这个示例仅作为一个基础框架,并可能存在一定的局限性和失效情况,尤其是当网站结构发生变化时。实际应用中,需要考虑到更多的边界条件和异常处理。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值