英文:
Parsing info revealed on button click
问题
我尝试解析这个页面: https://www.website.com/details/
有一个电话号码按钮 <a class="btn btn-danger show-number"> 隐藏了电话号码的一部分。点击时,ajax post 请求会带来完整的电话号码。
我怎样能在 Python 中模拟这个 post 请求?
import requests, time, json
from bs4 import BeautifulSoup
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
s = requests.Session()
response = s.get(
"https://www.mashina.kg/details/kia-sorento-64631fe26592a662606296", headers=headers
)
soup = BeautifulSoup(response.text, "lxml")
英文:
I'm trying to parse this page: https://www.website.com/details/
There is a phone number button <a class="btn btn-danger show-number"> hiding the part of the
phone number. On click, ajax post request brings in the complete phone number.
How can I fake this post request in python?
import requests, time, json
from bs4 import BeautifulSoup
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
s = requests.Session()
response = s.get(
"https://www.mashina.kg/details/kia-sorento-64631fe26592a662606296", headers=headers
)
soup = BeautifulSoup(response.text, "lxml")
答案1
得分: 2
尝试:
import requests
from bs4 import BeautifulSoup
url = 'https://www.mashina.kg/details/kia-sorento-64631fe26592a662606296'
headers = {
'X-Requested-With': 'XMLHttpRequest'
}
soup = BeautifulSoup(requests.get(url + '/givemereal', headers=headers).content, 'html.parser')
for n in soup.select('.number'):
print(n.text)
这将打印出电话号码。
英文:
Try:
import requests
from bs4 import BeautifulSoup
url = 'https://www.mashina.kg/details/kia-sorento-64631fe26592a662606296'
headers = {
'X-Requested-With': 'XMLHttpRequest'
}
soup = BeautifulSoup(requests.get(url + '/givemereal', headers=headers).content, 'html.parser')
for n in soup.select('.number'):
print(n.text)
This will print the telephone numbers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论