【问题标题】:request.get() is not workingrequest.get() 不工作
【发布时间】:2018-03-11 19:22:20
【问题描述】:

我正在尝试从我的 uni 网站上抓取一些数据,我正在使用 requests 和 lxml | html 为此。我曾经使用 beautifulsoup4,但它对我的使用速度不够快

这是我第一次使用 lxml,我收到了这个错误:

from lxml import html
import requests
import json
import logging

url = 'https://example.com/' 
url_ajax = "https://example.com//webapps/portal/execute/tabs/tabAction"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With': 'XMLHttpRequest'
}  
#data of url link
    'payload = {
    'user_id': 'myid',
    'password': 'mypass'
}

#data of cources (ajax call) 

course_data = {
    'action' : 'refreshAjaxModule',
    'modId' : '_27_1',
    'tabId' : '_1_1' , 
    'tab_tab_group_id' : '_1_1' 
}
# make sure that links are working fine
# Enabling debugging at http.client level (requests->urllib3->http.client)
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# the only thing missing will be the response.body which is not logged.
"""try: # for Python 3
    from http.client import HTTPConnection
except ImportError:
    from httplib import HTTPConnection
HTTPConnection.debuglevel = 1
logging.basicConfig() 
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
"""
# start the script 
session = requests.Session()
#go to the root url and post the username and password 
session.post(url ,headers=headers,data=payload ) 
# get the data of cources 
urlajax = session.post(url_ajax , headers=headers, data= course_data)   #get the ajax call

page = requests.get(urlajax)
page.json()   # This *call* raises an exception if JSON decoding fails
# here is my error 
content = page.content
tree = html.fromstring(content)
ga = tree.xpath('//div[@id="div_27_1"]//div[@id="_27_1termCourses__8_1"]/ul/li[1]/a/text()')
print(ga)

这是我的错误:

File "scrape.py", line 56, in <module>
     page = requests.get(urlajax)
 File "C:\Users\HozRifai\Desktop\WEBSCR~1\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
File "C:\Users\HozRifai\Desktop\WEBSCR~1\lib\site-packages\requests\api.py", line 58, in request
        return session.request(method=method, url=url, **kwargs)
File "C:\Users\HozRifai\Desktop\WEBSCR~1\lib\site-packages\requests\sessions.py", line 494, in request
        prep = self.prepare_request(req)
File "C:\Users\HozRifai\Desktop\WEBSCR~1\lib\site-packages\requests\sessions.py", line 437, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\HozRifai\Desktop\WEBSCR~1\lib\site-packages\requests\models.py", line 305, in prepare
        self.prepare_url(url, params)
File "C:\Users\HozRifai\Desktop\WEBSCR~1\lib\site-packages\requests\models.py", line 379, in prepare_url
        raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL '<Response [200]>': No schema supplied. Perhaps you meant http://<Response [200]>?

【问题讨论】:

    标签: python xmlhttprequest python-requests lxml


    【解决方案1】:

    检查这一行:

    url_ajax = "https://example.com//webapps/portal/execute/tabs/tabAction"
    

    您确定要在根 url 和 /webapps 部分之间包含两个 // 吗?

    【讨论】:

    • 是的,我的大学网站是...。我正在尝试重新设计它我使用 beautifuksoup4 尝试了相同的代码,并且一切正常:)
    【解决方案2】:

    错误的重要部分是最后一行:

    requests.exceptions.MissingSchema:无效的 URL '':未提供架构。也许你的意思是http://&lt;Response [200]&gt;

    这一行发生了什么

    page = requests.get(urlajax)
    

    我不认为 urlajax 是正确的类型,它正在变成字符串"&lt;Response 200&gt;"

    我不知道您要做什么——如果您想从该响应中获取信息,您需要在 urlajax 响应对象中查找它。对象本身不仅仅是返回的有效负载。

    【讨论】:

    • 是的。我想学习这学期要上的课程。顺便说一句,此代码也不起作用 session = requests.Session() r = session.post(url ,headers=headers,data=payload ) page = requests.get(r).text print(page)
    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2018-08-15
    • 2019-10-31
    相关资源
    最近更新 更多