Skip to content

Instantly share code, notes, and snippets.

@ZhangYet
Created September 23, 2019 12:26
Show Gist options
  • Save ZhangYet/9b3570762fb2729d404a2e2fa0561289 to your computer and use it in GitHub Desktop.
Save ZhangYet/9b3570762fb2729d404a2e2fa0561289 to your computer and use it in GitHub Desktop.
抓自如房租价格
import requests
from bs4 import BeautifulSoup
css = '''
<style>
i {
background-repeat: no-repeat;
background-size: auto 30px;
display: inline-block;
height: 30px;
position: relative;
top: 6px;
width: 19px;
z-index: 1;
}
</style>
</div>
'''
url = 'http://sz.ziroom.com/x/789608794.html'
page = requests.get(url)
content = page.text
parser = BeautifulSoup(content, 'html.parser')
# 从页面中抓取对应价钱的 div:找到 class == 'Z_price' 的 div
price_info: str = parser.find('div', class_='Z_price').__str__()
# 把 // 替换成 http://
price_info = price_info.replace('//static8.ziroom.com', 'http://static8.ziroom.com')
# 插入 css
price_info = price_info.replace('</div>', css)
# 保存为 html
with open('./price.html', 'w') as output:
output.write(price_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment