Python爬虫初体验
- 闲来无事,试着写一个小爬虫,功能是爬取一个网页上的url链接,话不多说上代码:
import requests
import re
def Find(string):
url = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+/[a-z,A-Z,0-9,/,.]+', string)
return url
def Url(string):
response = requests.get(string)
response.encoding = response.apparent_encoding
if response.status_code == 200:
return Find(response.text)
else:
return False
string =input("请输入一个带http的url链接:")
result =Url(string)
if False == result:
print("没有获取到信息")
else:
print("爬取到的url链接有:")
print(result)
- 总结:写python的代码还是很舒服的,没有那么多限制,想怎么写就怎么写.