网站抓取时返回欺骗结果/ 404?

时间:2020-04-25 06:55:56

标签: python web-scraping beautifulsoup python-requests

我正在尝试抓取以下site。我尝试使用request.get并用Beautiful Soup进行了解析,但是返回的结果与使用浏览器查看时返回的结果不同。我也直接调用他们正在使用的端点,但是返回404错误。我尝试使用标头,但这并没有解决。我该怎么解决?

这是我使用的代码:

import requests
import BeautifulSoup

headers = headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 X-Requested-With: XMLHttpRequest'}

URL = 'url'
x = requests.get(url, headers=headers)

上面的代码确实返回输出,但是它与网站的内容不同,即到出现的文章的链接

2 个答案:

答案 0 :(得分:2)

它使用ajax加载页面。我找到了API。 所有网址可能是:

url = "https://legitquest.com/Search/GetResultBySelectedSearchResult?caseText=AIR+1950+SC+1&type=citation&filter=&sortBy=1&formattedCitation=AIR+1950+SC+1&removeFilter=&filterValueList=&_={}".format(str(time.time()).replace(".","")[:-4])

enter image description here

但是由于某些原因,它也无法抓取页面。(此页面使用严格的规则来防止抓取)

即使我使用了正确的网址,也无法获取它:

enter image description here


强烈建议您使用selenium。这样会更容易。


我明白了:

import requests
import time

headers = {
    "X-Requested-With": "XMLHttpRequest"
}
url = 'https://legitquest.com/Search/GetResultBySelectedSearchResult?caseText=AIR+1950+SC+1&type=citation&filter=&sortBy=1&formattedCitation=AIR+1950+SC+1&removeFilter=&filterValueList=&_={}'.format(str(time.time()).replace(".","")[:-4])
x = requests.get(url,headers=headers)
print(x.json()["CaseDetails"][0]["LinkText"])

结果:

Sheth Maneklal Mansukhbhai V. Messrs. Hormusji Jamshedji Ginwallaand Sons

json格式:

{
    'filterList': '',
    'filterValueList': '',
    'caseText': 'AIR 1950 SC 1',
    'currentpage': 1,
    'CaseCount': 1,
    'openPopup': False,
    'UserId': '',
    'IsSubscribed': False,
    'IsMobileDevice': False,
    'CaseDetails': [{
        'LinkText': 'Sheth Maneklal Mansukhbhai V. Messrs. Hormusji Jamshedji Ginwallaand Sons',
        'PartyName': 'sheth-maneklal-mansukhbhai-vs-messrs.-hormusji-jamshedji-ginwallaand-sons',
        'SearchString': None,
        'CaseId': 21763,
        'EncryptedId': '1EBBB',
        'CourtName': 'Supreme Court Of India',
        'Id': 125883,
        'CourtId': 1,
        'CaseType': None,
        'HeadNotes': None,
        'Judges': "HON'BLE MR. JUSTICE M.C. MAHAJAN<BR />HON'BLE MR. JUSTICE SAIYID FAZAL ALI<BR />HON'BLE MR. JUSTICE B.K. MUKHERJEA",
        'DateOfJudgment': '21-03-1950',
        'Judgment': None,
        'OrderByDateTime': '/Date(-624326400000)/',
        'CaseNo': None,
        'Advocates': None,
        'CitationText': '',
        'CitatedCount': 0,
        'CopyText': None,
        'AlternativeCitation': '(1950) SCR 75 ; AIR 1950 SC 1 ; 1950 SCJ 317 ; (1950) 63 LW 495',
        'Petitioner': None,
        'Responder': None,
        'Citation': None,
        'Question': None,
        'HighlightedText': '',
        'IsFoundText': True,
        'IsOverruledExist': False,
        'IsDistinguishedExist': False,
        'IsOtherStatusExist': True,
        'OtherStatusImgUrl': 'https://www.legitquest.com/Content/themes/treatment/referred.svg',
        'OverruledImgUrl': None,
        'DistinguishedImgUrl': None,
        'BookmarkId': 0,
        'Chart': None,
        'CaseCitedCount': None,
        'SnapShot': None
    }]
}

答案 1 :(得分:0)

关于此操作:

&&

我得到了您需要的结果

enter image description here