首页 > 解决方案 > Python - 查看机器人?

问题描述

我正在尝试为 viewbot 编写一些代码。

代码:

import requests
from bs4 import BeautifulSoup
import html5lib
import urllib
import argparse, os, time
import urllib.parse, random

headers = {
    'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Mobile Safari/537.36'
    }
login_data = {
    'login': 'xxx',
    'pass': 'xxx',
    'back_url': '' 
    }

现场登录正常工作。

列出人员:

def getPeopleLinks(page):
    links = []
    for link in soups.find_all('a'):
        url = link.get('href')
        if url:
            if 'profile/' in url:
                links.append(url)
    return links

在职的...

和其他代码:

with requests.Session() as session:
        url = "https://xxxxxx.com/Login/?form_login=1"

        post = session.post(url, data=login_data, headers=headers)
print (post.status_code)
print (post.cookies)
r = session.get("https://xxxxxxx.com/online/GIRL")
print (r.status_code)
print (r.cookies)
soups = BeautifulSoup(r.content, 'html5lib')
x = getPeopleLinks(soups)
print(x)
print("http://www.xxxxx.com"+ x[2])
for link in x: 
        urllib.request.urlopen("http://www.xxxxxxx.com"+link)
print(link)

登录:正常工作。
在线用户列表:正常工作;我得到所有配置文件列表。

我认为这是一个问题:

for link in x: 
        urllib.request.urlopen("http://www.xxxxxxx.com"+link)
print(link)

我通过手机登录了另一个帐户,我的个人资料在列表中,但 PC 上的机器人没有查看我的个人资料。

标签: pythonwebloginurllib2

解决方案


这是因为url的语法有问题。或者可能是因为此特定代码中可能存在问题,请尝试以下操作:

import urllib.request
for path in paths:
    url = 'http://example.com/view-online-profiles/' + path
    page = urllib.request.urlopen(url)
    print(page.read())

或者你也可以按照另一种方式:

import requests
for path in paths:
    url = 'http://example.com/view-online-profiles/' + path
    page = requests.get(url)
    print(page) # Would return response object, can obtain status_code or body

推荐阅读