【问题标题】:Website returning spoofed result/404 while scraping?网站在抓取时返回欺骗结果/404?
【发布时间】:2020-08-08 20:01:18
【问题描述】:

我正在尝试抓取以下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)

上面的代码确实返回了输出,但是它没有和网站一样的内容,即出现的一篇文章的链接

【问题讨论】:

  • 分享您尝试过的内容,而不是图片链接。
  • 要获取结果的url吗?
  • 不,只是标题。

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

它使用 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])

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

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


强烈推荐你使用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
    }]
}

【讨论】:

    【解决方案2】:

    关于这样做:

    url = 'https://legitquest.com/Home/GetCaseDetails?searchType=citation&publisher=AIR%201950%20SC%201'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36'}
    page_html = requests.get(url,headers=headers)
    print("Status Code : ")
    print(page_html.status_code)
    page_soup = soup(page_html.content,features="lxml")
    

    我得到了你需要的结果

    【讨论】:

    • 没有。它只是返回一个空列表。另外,我需要使用该 URL 搜索时出现的文章链接。
    • 我不确定,但我认为他们可能在欺骗结果?
    • 这是他们在你面前的状态码,然后你可以搜索你需要的元素......如果你只需要链接,你可以搜索他们可以帮助你的扩展工具:)
    • 嘿!我相信它没有回来。能否在检索内容时检查是否得到以下文章名称:Sheth Maneklal Mansukhbhai V. Messrs. Hormusji Jamshedji Ginwallaand Sons. 它返回的是 200,但这并不意味着它是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多