首页 > 试题广场 >

4 [问答]编写代码读取搜狐网页首页内容。

[问答题]
编写代码读取搜狐网页首页内容。
import requests
content = request.get(url=url).content
print(content.decode())
编辑于 2018-09-21 10:11:54 回复(2)
from urllib.request import urlopen

def request_sohu(address):
    with urlopen(address) as resp:
        html = resp.read()
    print(html.decode('utf-8'))

if __name__ == "__main__":
    request_sohu("https://www.sohu.com")
可以用内置的urllib.request方法,也可以用第三方包requests
发表于 2020-06-06 11:18:02 回复(0)
鳴头像
import urllib.request
url="http://www.sohu.com/" headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=url,headers=headers)
my_data = urllib.request.urlopen(req) with open('sohu.html', 'wb') as f:
    read_data = my_data.read()
    f.write(read_data)
发表于 2018-10-28 22:58:21 回复(0)

from selenium import webdriver


driver = webdriver.Chrome()#打开google窗口

driver.implicitly_wait(3)#等待三秒

driver.get(‘https://www.sohu.com’)#获取搜狐网站

driver.quit()#退出窗口


发表于 2019-06-05 15:39:35 回复(1)