English 中文(简体)
声望不要退回超文本体
原标题:Beautiful soup not returning HTML body

I made a script to scrape data from a website. Everything was fine until today when I ran my script and didn t get any html output from Beautiful Soup. The code is:

import requests
from bs4 import BeautifulSoup

url =  https://www.immobiliare.it 
headers = {     "User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"   }

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content,  html.parser )
soup.encode( utf-8 )

print(soup)

我甚至利用灯塔进行审判:

soup = BeautifulSoup(response.content,  lxml )

但是,产出中的所有Im都只是一个数字,总是在45,105,116,34,125,60,47,115,99,114,105,112,116,62,60,47,98,111,100,121,62,60,47,104,116,109,108,62

I even tried changing headers and IP but nothing changes. When I print the STATUS code it shows 200 though.

It doesn t show html body and tags, it just shows numbers and commas. It doesn t even have <html> and <body> tags. This problem happens only one this website even though the same script was working few days ago.

问题回答

I believe the problem is the user-agent.
Without user-agent, I was able to get proper result.
Also, you should use

soup = soup.encode( utf-8 )

而不是

soup.encode( utf-8 )

to set the encoded html into soup.
Here is the final code.

import requests
from bs4 import BeautifulSoup

url =  https://www.immobiliare.it 

response = requests.get(url)
soup = BeautifulSoup(response.content,  html.parser )
soup = soup.encode( utf-8 )

print(soup)

结果





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签