Python爬取114Best电话号码:技巧与示例代码
由于该网站有反爬机制,需要使用一些技巧来获取电话号码。以下是一个基于Python的示例代码:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
def get_phone_numbers(url):
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
numbers = []
for item in soup.select('.tel-list li'):
if 'style='display:none;' in str(item):
continue
phone_number = item.select_one('.tel').text.strip()
numbers.append(phone_number)
return numbers
if __name__ == '__main__':
url = 'https://www.114best.com/tel/'
numbers = get_phone_numbers(url)
print(numbers)
该代码使用requests库和BeautifulSoup库来获取网页内容,并解析出电话号码。需要注意的是,该网站在页面中使用了style='display:none;'来隐藏一些电话号码,因此需要在解析时进行判断并跳过。另外,由于该网站有反爬机制,建议加上一个随机的User-Agent头来模拟浏览器访问。
原文地址: https://www.cveoy.top/t/topic/mZeK 著作权归作者所有。请勿转载和采集!