【问题标题】:POST Request Python Web Scraping: Get URLs from TagPOST 请求 Python Web Scraping:从标签中获取 URL
【发布时间】:2021-09-16 21:51:11
【问题描述】:

您好,我是 python 和网络抓取的新手。从我的脚本中,我想发布请求,然后从中获取网址。但是我遇到了一个问题,我无法从 a 标签中获取网址。当我检查网站时,我可以看到 URL,但在我的脚本中它显示为 javascript。 所以,我正在寻找一些想法来解决这个问题。谢谢。

from bs4 import BeautifulSoup

url = 'http://www.adassothai.com/index.php/main/ad_expenditure/'
myobj = {'y': '2014'}

x = requests.post(url, data = myobj)
soup = BeautifulSoup(x.text, 'html.parser')
print(soup)

【问题讨论】:

    标签: javascript python-2.7 web-scraping beautifulsoup python-requests


    【解决方案1】:

    使用后请求数据为 json 格式,因此您可以使用它来提取数据 从中只需在调用 URL 后使用 .json() 方法,并且标头也是可选的

    from bs4 import BeautifulSoup
    import requests
    headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
            "Referer": "http://www.adassothai.com/index.php/main/ad_expenditure/"
    }
    url = 'http://www.adassothai.com/index.php/main/ad_expenditure/'
    myobj = {'y': '2014'}
    
    res= requests.post(url, data = myobj)
    data= res.json()['data']
    
    soup=BeautifulSoup(data,"lxml")
    

    【讨论】:

      猜你喜欢
      • 2016-06-03
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多