[英]I am new to django and when I am trying to python manage.py runserver... I was getting an error below... what I have to do now? please let me know
[英]I am trying to do web scraping with Python and have made a request like below and got the response. but don't know how to process it
我想從響應中提取鏈接。
請求:
import requests
headers = {
'authority': 'www.xxxxxx.net',
'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
'accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
'x-requested-with': 'XMLHttpRequest',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/95.0.4638.54 Safari/537.36',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.xxxxxx.net/',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'cookie': 'bnState={"impressions":1,"delayStarted":0};
pnState="impressions":2,"delayStarted":1635254046187}',
}
params = (
('alt', 'json-in-script'),
('max-results', '12'),
('start-index', '13'),
('callback', 'jQuery22404432064732296963_1635254045161'),
('_', '1635254045166'),
)
url='https://www.xxxxxx.net/feeds/posts/default?alt=json-in-script&max-results=12&start-index=1&callback=jQuery22404432064732296963_1635254045161&_=1635254045166'
response = requests.get('https://www.xxxxxx.net/feeds/posts/default', params=params)
打印(響應。文本)
回應:
注意:請告訴我如何處理響應。 另請注意,出於隱私原因,我更改了網址。
在此先感謝您的幫助。
這是一個包含 unicode 內容的“ascii”字符串。 您需要先將其轉換為普通的“unicode”字符串。 嘗試這個:
html_content = bytes(response.text, "ascii").decode("unicode-escape")
之后,您將獲得“HTML/XML”格式的普通字符串。 然后你就可以使用“ BeautifulSoup4 ”來解析它並獲取你需要的內容。
如果您正在進行網絡抓取,我強烈建議您使用BeautifulSoup庫來解析您的響應。 如下圖初始化:
from bs4 import BeautifulSoup
response = "" # your response
soup = BeautifulSoup(response) # Parse response and save it into a variable
獲取所有href:
hrefs = soup.find_all(href=True)
links = [i['href'] for i in hrefs] # An array with all your links
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.